package xujiwen.transport.util.xml;

//import xujiwen.transport.util.Configuration;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * 
 * @author �򳿻�
 * 
 */

public class Configuration{	
	private final SAXReader reader = new SAXReader();	// xml����
	private XMLWriter writer = null;

	private final OutputFormat format = OutputFormat.createPrettyPrint();

	private Document document = null;

	private static final String filename = xujiwen.transport.util.StaticData.WEBINF_PATH
			+ "info.xml";

	public Configuration() {
		// init();
	}

	/**
	 * ��ʼ�������ļ�
	 */
	public String init() {
		this.format.setEncoding("GBK");// ����XML�ļ��ı����ʽ
		final File file = new File(Configuration.filename);
		if (!file.exists()) {
			System.out.println("������");
		}
		return null;
	}

	public Document getDocument() {
		try {
			final File file = new File(Configuration.filename);
			this.reader.setEncoding("GBK");
			this.document = this.reader.read(file);
			this.document.setXMLEncoding("GBK");
		} catch (final DocumentException e) {
			e.printStackTrace();
		}
		return this.document;
	}

	public void write(final Document _document) {
		try {
			this.writer = new XMLWriter(new FileWriter(Configuration.filename),
					this.format);
			this.writer.write(_document);
		} catch (final IOException e) {
			e.printStackTrace();
		}
	}

	public void close() {
		try {
			if (this.writer != null) {
				this.writer.close();
			}
		} catch (final IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(final String[] args) {

	}
}
